package org.rbac.serv;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Query;
import org.rbac.content.RbacApplicationContent;
import org.rbac.po.PubConst;
import org.rbac.vo.Const;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 系统常量(数据字典)
 * 
 * <pre>
 * 项目名称：rbac
 * 类名称：ConstServices
 * 类描述：系统常量(数据字典)
 * 创建人：唐家林(tjl)
 * 创建时间：2014年9月27日　下午3:01:53
 * 修改人：
 * 修改时间：
 * 修改备注：
 * @version
 * </pre>
 */
@Service
public class ConstServices extends CommonServices {
    @Autowired
    private RbacApplicationContent rbacContent;

	
	public void saveConst(Const c,HttpServletRequest req) throws Exception {
		this.editConst(c);
		rbacContent.loadDictionary(req.getServletContext());
	}

	/**
	 * 修改
	 * 
	 * @param c
	 *            修改的数据
	 * @throws Exception
	 */
	public void moidifyConst(Const c,HttpServletRequest req) throws Exception {
		this.editConst(c);
		rbacContent.loadDictionary(req.getServletContext());
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 *            删除的对象数据主键
	 * @throws Exception
	 */
	public void deleteConst(Integer[] ids,HttpServletRequest req) throws Exception {
		String hql = "DELETE FROM PubConst pc WHERE pc.id IN (:ids)";
		Query q = getDao().getSession().createQuery(hql);
		q.setParameterList("ids", ids);
		q.executeUpdate();
		rbacContent.loadDictionary(req.getServletContext());
	}

	/**
	 * 编辑(新增或修改)
	 * 
	 * @param c
	 *            操作的数据
	 */
	public void editConst(Const c) {
		if (null == c) {
			return;
		}
		PubConst cons = null;
		if (null == c.getId()) {
			cons = new PubConst();
		} else {
			cons = (PubConst) getDao().get(PubConst.class, c.getId());
		}
		// 属性值拷贝(拷贝r对象的属性值到role对象中对应名称的属性,且不拷贝id和RCdate的值)
		BeanUtils.copyProperties(c, cons, new String[] { "id" });

		if (null == c.getId()) {
			getDao().save(cons);
		} else {
			getDao().update(cons);
		}
	}
}
